﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace CopyPlaylistFiles
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            //openFile.Filter = "ASX,B4S,M3U,M3U8,PLS,WPL";
            openFile.Filter = "M3U playlist|*.m3u";
            openFile.Title = "Select Playlisst file";
            DialogResult searchFile = openFile.ShowDialog();
            if (searchFile == DialogResult.OK)
            {
                textBox1.Text = openFile.FileName;
            }
        }

        private void buttonStoreFolder_Click(object sender, EventArgs e)
        {
            DialogResult folderResult = folderBrowser.ShowDialog();
            if (folderResult == DialogResult.OK)
            {
                textBox2.Text = folderBrowser.SelectedPath;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            String line = "";

            if (textBox1.Text != "" && textBox2.Text != "")
            {
                // open file
                TextReader textReader = new StreamReader(textBox1.Text);
                string fileName = "";
                
                // read the entire file
                while ((line = textReader.ReadLine()) != null)
                {
                    // we are interested only in the lines that are not staring with # character
                    if (line[0] != '#')
                    {
                        // get the file name
                        string[] str = line.Split('\\');
                        fileName = str[str.Length - 1];
                        
                        // copy file
                        try
                        {
                            if (File.Exists(line))
                                File.Copy(line, textBox2.Text + '\\' + fileName, true);
                            else
                            {
                                if (File.Exists("c:" + line))
                                    // try from drive c
                                    File.Copy("c:" + line, textBox2.Text + '\\' + fileName, true);
                                else
                                    throw new System.Exception(string.Format("file : {0} can not be found", line));
                            }
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show("Exception: " + ex.Message);
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show("Exception: " + exception.Message);
                        }
                    }
                }
            }
            else
                MessageBox.Show("Please select winamp playlist file and save directory.");
        }

    }
}
